﻿#ifndef DATABUFFER_H
#define DATABUFFER_H

#include "types.h"

class DataBuffer {
public:
    explicit DataBuffer(int len = 1024);
    explicit DataBuffer(QByteArray &buffer);
    explicit DataBuffer(u8 *data, int len);
    explicit DataBuffer(DataBuffer *db);
    virtual ~DataBuffer();
public:
    void append_s32(int32_t data);
    void append_s16(int16_t data);
    void append_s8(int8_t data);
    void append_u32(uint32_t data);
    void append_u16(uint16_t data);
    void append_u8(uint8_t data);
    void append_u24(uint32_t data);
    void append_buff(uint8_t *buff, int len);
    void append_bytes(QByteArray &data);
    void append_float(float data);
    u8   value_u8(int index = 0);
    u16  value_u16(int index = 0);
    u32  value_u24(int index = 0);
    u32  value_u32(int index = 0);
    s8   value_s8(int index = 0);
    s16  value_s16(int index = 0);
    s32  value_s32(int index = 0);
    float value_float(int index = 0);
    void skip(int offset);
    virtual void clear() {
        rd_index = 0;
        Data.clear();
    }
    int size() {
        return Data.size();
    }
    void resize(int size) {
        Data.resize(size);
    }
    char *buffer(){
        return Data.data();
    }

    char *curr_buffer(){
        return buffer() + rd_index;
    }

    QByteArray &byte_array(){
        return Data;
    }
    int offset(){
        if (b_end) {
            return size();
        }
        return rd_index;
    }
    int remain(){
        return (size() - offset());
    }
    int index(){
        return rd_index;
    }
    bool end(){
        return b_end;
    }
protected:
    QByteArray Data;
    int rd_index;
    bool b_end;
};

#endif // DATABUFFER_H
